home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / Filezilla Server / FileZilla_Server-0_9_41.exe / source / interface / MainFrm.cpp < prev    next >
C/C++ Source or Header  |  2012-02-22  |  36KB  |  1,350 lines

  1. // FileZilla Server - a Windows ftp server
  2.  
  3. // Copyright (C) 2002-2004 - Tim Kosse <tim.kosse@gmx.de>
  4.  
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9.  
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. // MainFrm.cpp : Implementierung der Klasse CMainFrame
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "FileZilla server.h"
  24. #include "misc/led.h"
  25. #include "MainFrm.h"
  26. #include "../iputils.h"
  27. #include "../platform.h"
  28. #include "statusview.h"
  29. #include "usersdlg.h"
  30. #include "GroupsDlg.h"
  31. #include "options.h"
  32. #include "usersview.h"
  33. #include "userslistctrl.h"
  34. #include "misc/systemtray.h"
  35. #include "offlineaskdlg.h"
  36. #include "../version.h"
  37. #include "AdminSocket.h"
  38. #include "OptionsDlg.h"
  39. #include "ConnectDialog.h"
  40. #include "mainfrm.h"
  41. #include "../defs.h"
  42. #include "OutputFormat.h"
  43.  
  44. #if defined(_DEBUG) && !defined(MMGR)
  45. #define new DEBUG_NEW
  46. #undef THIS_FILE
  47. static char THIS_FILE[] = __FILE__;
  48. #endif
  49.  
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CMainFrame
  52.  
  53. IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
  54.  
  55. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  56.     //{{AFX_MSG_MAP(CMainFrame)
  57.     ON_WM_CREATE()
  58.     ON_WM_SETFOCUS()
  59.     ON_WM_SIZE()
  60.     ON_WM_CLOSE()
  61.     ON_COMMAND(ID_EDIT_SETTINGS, OnEditSettings)
  62.     ON_COMMAND(ID_ACTIVE, OnActive)
  63.     ON_UPDATE_COMMAND_UI(ID_ACTIVE, OnUpdateActive)
  64.     ON_WM_SYSCOMMAND()
  65.     ON_COMMAND(ID_TRAY_EXIT, OnTrayExit)
  66.     ON_COMMAND(ID_TRAY_RESTORE, OnTrayRestore)
  67.     ON_COMMAND(ID_LOCK, OnLock)
  68.     ON_UPDATE_COMMAND_UI(ID_LOCK, OnUpdateLock)
  69.     ON_WM_TIMER()
  70.     ON_COMMAND(ID_USERS, OnMenuEditUsers)
  71.     ON_COMMAND(ID_GROUPS, OnMenuEditGroups)
  72.     ON_COMMAND(ID_FILE_CONNECT, OnFileConnect)
  73.     ON_COMMAND(ID_FILE_DISCONNECT, OnFileDisconnect)
  74.     ON_UPDATE_COMMAND_UI(ID_FILE_DISCONNECT, OnUpdateFileDisconnect)
  75.     ON_UPDATE_COMMAND_UI(ID_EDIT_SETTINGS, OnUpdateEditSettings)
  76.     ON_UPDATE_COMMAND_UI(ID_MENU_EDIT_USERS, OnUpdateMenuEditUsers)
  77.     ON_UPDATE_COMMAND_UI(ID_MENU_EDIT_GROUPS, OnUpdateMenuEditGroups)
  78.     ON_UPDATE_COMMAND_UI(ID_USERS, OnUpdateUsers)
  79.     ON_UPDATE_COMMAND_UI(ID_GROUPS, OnUpdateGroups)
  80.     ON_COMMAND(ID_MENU_EDIT_USERS, OnMenuEditUsers)
  81.     ON_COMMAND(ID_MENU_EDIT_GROUPS, OnMenuEditGroups)
  82.     ON_COMMAND(ID_USERLISTTOOLBAR_DISPLAYLOGICAL, OnDisplayLogicalNames)
  83.     ON_COMMAND(ID_USERLISTTOOLBAR_DISPLAYPHYSICAL, OnDisplayPhysicalNames)
  84.     ON_UPDATE_COMMAND_UI(ID_USERLISTTOOLBAR_DISPLAYLOGICAL, OnUpdateDisplayLogicalNames)
  85.     ON_UPDATE_COMMAND_UI(ID_USERLISTTOOLBAR_DISPLAYPHYSICAL, OnUpdateDisplayPhysicalNames)
  86.     ON_COMMAND(ID_USERLISTTOOLBAR_SORT, OnDisplaySortMenu)
  87.     ON_NOTIFY(TBN_DROPDOWN, AFX_IDW_TOOLBAR, OnToolbarDropDown)
  88.     ON_COMMAND(ID_DISPLAY_SORTBYUSERID, OnDisplaySortByUserid)
  89.     ON_COMMAND(ID_DISPLAY_SORTBYACCOUNT, OnDisplaySortByAccount)
  90.     ON_COMMAND(ID_DISPLAY_SORTBYIP, OnDisplaySortByIP)
  91.     ON_UPDATE_COMMAND_UI(ID_DISPLAY_SORTBYUSERID, OnUpdateDisplaySortByUserid)
  92.     ON_UPDATE_COMMAND_UI(ID_DISPLAY_SORTBYACCOUNT, OnUpdateDisplaySortByAccount)
  93.     ON_UPDATE_COMMAND_UI(ID_DISPLAY_SORTBYIP, OnUpdateDisplaySortByIP)
  94.     ON_WM_DESTROY()
  95.     //}}AFX_MSG_MAP
  96.     ON_WM_ERASEBKGND()
  97. END_MESSAGE_MAP()
  98.  
  99. static UINT indicators[] =
  100. {
  101.     ID_SEPARATOR,           // Statusleistenanzeige
  102.     ID_INDICATOR_RECVCOUNT,
  103.     ID_INDICATOR_RECVRATE,
  104.     ID_INDICATOR_SENDCOUNT,
  105.     ID_INDICATOR_SENDRATE,
  106.     ID_INDICATOR_RECVLED,
  107.     ID_INDICATOR_SENDLED,
  108.     ID_SEPARATOR
  109. };
  110.  
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CMainFrame Konstruktion/Zerst÷rung
  113.  
  114. CMainFrame::CMainFrame(COptions *pOptions)
  115. {
  116.     ASSERT(pOptions);
  117.     s_winClassName = 0;
  118.     nTrayNotificationMsg_ = RegisterWindowMessage(_T("FileZilla Server Tray Notification Message"));
  119.     m_bQuit = FALSE;
  120.     m_nSendCount = 0;
  121.     m_nRecvCount = 0;
  122.     m_lastchecktime = GetTickCount();
  123.     m_lastreaddiff = 0;
  124.     m_lastwritediff = 0;
  125.     m_nOldRecvCount = 0;
  126.     m_nOldSendCount = 0;
  127.     m_pOptions = pOptions;
  128.     m_pAdminSocket = NULL;
  129.     m_nServerState = 0;
  130.     m_nEdit = 0;
  131.  
  132.     m_pOptionsDlg = 0;
  133.     m_pUsersDlg = 0;
  134.     m_pGroupsDlg = 0;
  135.  
  136.     m_nReconnectTimerID = 0;
  137.     m_nReconnectCount = 0;
  138. }
  139.  
  140. CMainFrame::~CMainFrame()
  141. {
  142.     delete m_pOptions;
  143.     m_pOptions = 0;
  144.     CloseAdminSocket(false);
  145.     delete [] s_winClassName; //Does seem to crash
  146. }
  147.  
  148. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  149. {
  150.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  151.         return -1;
  152.     
  153.     SetupTrayIcon();
  154.  
  155.     if (!m_wndReBar.Create(this))
  156.         return -1;
  157.  
  158.     if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP
  159.         | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
  160.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  161.     {
  162.         TRACE0("Could not create Toolbar 1\n");
  163.         return -1;      // Fehler bei Erstellung
  164.     }
  165.     
  166.     if (!m_wndUserListToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_ALIGN_LEFT
  167.         | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
  168.         !m_wndUserListToolBar.LoadToolBar(IDR_USERLISTTOOLBAR))
  169.     {
  170.         TRACE0("Could not create Toolbar 2\n");
  171.         return -1;      // Fehler bei Erstellung
  172.     }
  173.     m_wndUserListToolBar.GetToolBarCtrl().SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS);
  174.     DWORD dwStyle = m_wndUserListToolBar.GetButtonStyle(m_wndUserListToolBar.CommandToIndex(ID_USERLISTTOOLBAR_SORT));
  175.     dwStyle |= TBSTYLE_DROPDOWN;
  176.     m_wndUserListToolBar.SetButtonStyle(m_wndUserListToolBar.CommandToIndex(ID_USERLISTTOOLBAR_SORT), dwStyle);
  177.  
  178.     m_wndReBar.AddBar(&m_wndToolBar);
  179.     m_wndReBar.AddBar(&m_wndUserListToolBar);
  180.     m_wndReBar.GetReBarCtrl().MinimizeBand(0);
  181.     
  182.     if (!m_wndStatusBar.Create(this) ||
  183.         !m_wndStatusBar.SetIndicators(indicators,
  184.           sizeof(indicators)/sizeof(UINT)))
  185.     {
  186.         TRACE0("Statusleiste konnte nicht erstellt werden\n");
  187.         return -1;      // Fehler bei Erstellung
  188.     }
  189.  
  190.     // The last statusbar pane is a fake one, it has zero width.
  191.     m_wndStatusBar.SetPaneInfo(7, 0, SBPS_NOBORDERS, 0);
  192.  
  193.     CRect rect;
  194.     m_wndStatusBar.GetItemRect(m_wndStatusBar.CommandToIndex(ID_INDICATOR_RECVLED), rect);  
  195.  
  196.     //Create the first LED control
  197.     m_RecvLed.Create(_T(""), WS_VISIBLE|WS_CHILD, rect, &m_wndStatusBar, m_wndStatusBar.CommandToIndex(ID_INDICATOR_RECVLED)); 
  198.     m_RecvLed.SetLed( CLed::LED_COLOR_GREEN, CLed::LED_OFF, CLed::LED_ROUND);
  199.  
  200.     //Create the second LED control
  201.     m_SendLed.Create(_T(""), WS_VISIBLE|WS_CHILD, rect, &m_wndStatusBar, m_wndStatusBar.CommandToIndex(ID_INDICATOR_SENDLED)); 
  202.     m_SendLed.SetLed( CLed::LED_COLOR_RED, CLed::LED_OFF, CLed::LED_ROUND);
  203.  
  204.     m_wndStatusBar.GetItemRect(m_wndStatusBar.CommandToIndex(ID_INDICATOR_SENDLED), &rect);
  205.     m_wndStatusBar.SetPaneInfo(m_wndStatusBar.CommandToIndex(ID_INDICATOR_RECVLED),ID_INDICATOR_RECVLED,SBPS_NOBORDERS,6);
  206.     m_wndStatusBar.SetPaneInfo(m_wndStatusBar.CommandToIndex(ID_INDICATOR_SENDLED),ID_INDICATOR_SENDLED,SBPS_NOBORDERS,6);
  207.  
  208.     ShowStatus(GetVersionString(), 0);
  209.     ShowStatus(_T("Copyright 2001-2012 by Tim Kosse (tim.kosse@filezilla-project.org)"), 0);
  210.  
  211.     m_nTimerID = SetTimer(7777, 10000, 0);
  212.     m_nRateTimerID = SetTimer(7778, 1000, 0);
  213.  
  214.     SetStatusbarText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_SENDCOUNT), _T("0 bytes sent"));
  215.     SetStatusbarText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_RECVCOUNT), _T("0 bytes received"));
  216.     SetStatusbarText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_RECVRATE), _T("0 B/s"));
  217.     SetStatusbarText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_SENDRATE), _T("0 B/s"));
  218.  
  219.     CConnectDialog dlg(m_pOptions);
  220.     if (!m_pOptions->GetOptionVal(IOPTION_ALWAYS) && dlg.DoModal() != IDOK)
  221.         return 0;
  222.  
  223.     m_pAdminSocket = new CAdminSocket(this);
  224.     ShowStatus("Connecting to server...", 0);
  225.     DoConnect();
  226.  
  227.     return 0;
  228. }
  229.  
  230. //////////////////
  231. // Helper function to register a new window class based on an already
  232. // existing window class, but with a different name and icon. 
  233. // Returns new name if successful; otherwise NULL.
  234. //
  235. static bool RegisterSimilarClass(LPCTSTR lpszNewClassName,
  236.     LPCTSTR lpszOldClassName, UINT nIDResource)
  237. {
  238.     // Get class info for old class.
  239.     //
  240.     HINSTANCE hInst = AfxGetInstanceHandle();
  241.     WNDCLASS wc;
  242.     if (!::GetClassInfo(hInst, lpszOldClassName, &wc)) {
  243.         TRACE("Can't find window class %s\n", lpszOldClassName);
  244.         return false;
  245.     }
  246.  
  247.     // Register new class with same info, but different name and icon.
  248.     //
  249.     wc.lpszClassName = lpszNewClassName;
  250.     wc.hIcon = ::LoadIcon(hInst, MAKEINTRESOURCE(nIDResource));
  251.     if (!AfxRegisterClass(&wc)) {
  252.         TRACE("Unable to register window class%s\n", lpszNewClassName);
  253.         return false;
  254.     }
  255.     return true;
  256. }
  257.  
  258. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  259. {
  260.     if( !CFrameWnd::PreCreateWindow(cs) )
  261.         return FALSE;
  262.     
  263.     // ZU ERLEDIGEN: ─ndern Sie hier die Fensterklasse oder das Erscheinungsbild, indem Sie
  264.     //  CREATESTRUCT cs modifizieren.
  265.  
  266.     cs.lpszClass = AfxRegisterWndClass(0);
  267.  
  268.     cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
  269.  
  270.     //Change the window class name
  271.     if (!s_winClassName) 
  272.     {
  273.         s_winClassName = new TCHAR[_tcslen(_T("FileZilla Server Main Window")) + 1];
  274.         _tcscpy(s_winClassName, _T("FileZilla Server Main Window"));
  275.         RegisterSimilarClass(s_winClassName, cs.lpszClass, IDR_MAINFRAME);
  276.     }
  277.     cs.lpszClass = s_winClassName;
  278.  
  279.     return TRUE;
  280. }
  281.  
  282. /////////////////////////////////////////////////////////////////////////////
  283. // CMainFrame Nachrichten-Handler
  284. void CMainFrame::OnSetFocus(CWnd* pOldWnd)
  285. {
  286.     // Fokus an das Ansichtfenster weitergeben
  287. }
  288.  
  289. BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
  290. {
  291.     // andernfalls die Standardbehandlung durchfⁿhren
  292.     return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  293. }
  294.  
  295. BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
  296. {
  297.     CRect rect;
  298.     GetClientRect(rect);
  299.  
  300.     // Unterteiltes Fenster erstellen
  301.     if (!m_wndSplitter.CreateStatic(this, 2, 1))
  302.         return FALSE;
  303.     if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CStatusView), CSize(1, rect.Height() - 150), pContext))
  304.     {
  305.         m_wndSplitter.DestroyWindow();
  306.         return FALSE;
  307.     }
  308.     m_pStatusPane = (CStatusView*)m_wndSplitter.GetPane(0, 0);
  309.  
  310.     if (!m_wndSplitter.CreateView(1, 0, RUNTIME_CLASS(CUsersView), CSize(1, 150), pContext))
  311.     {
  312.         m_wndSplitter.DestroyWindow();
  313.         return FALSE;
  314.     }
  315.     m_pUsersPane = (CUsersView*) m_wndSplitter.GetPane(1, 0);
  316.  
  317.     // Filename display option.
  318.     GetUsersPane()->m_pListCtrl->SetDisplayPhysicalNames(m_pOptions->GetOptionVal(IOPTION_FILENAMEDISPLAY) != 0);
  319.  
  320.     // Set layout options.
  321.     int sortInfo = (int)m_pOptions->GetOptionVal(IOPTION_USERSORTING);
  322.     GetUsersPane()->m_pListCtrl->SetSortColumn(sortInfo & 0x0F, sortInfo >> 4);
  323.  
  324.     return CFrameWnd::OnCreateClient(lpcs, pContext);
  325. }
  326.  
  327. CStatusView* CMainFrame::GetStatusPane()
  328. {
  329.     return m_pStatusPane;
  330. }
  331.  
  332. CUsersView* CMainFrame::GetUsersPane()
  333. {
  334.     return m_pUsersPane;
  335. }
  336.  
  337. void CMainFrame::OnSize(UINT nType, int cx, int cy) 
  338. {
  339.     if (m_wndStatusBar.GetSafeHwnd())
  340.     {
  341.         if (nType!=SIZE_MAXIMIZED)
  342.             m_wndStatusBar.SetPaneInfo(m_wndStatusBar.CommandToIndex(ID_INDICATOR_SENDLED),ID_INDICATOR_SENDLED,SBPS_NOBORDERS,0);
  343.         else
  344.             m_wndStatusBar.SetPaneInfo(m_wndStatusBar.CommandToIndex(ID_INDICATOR_SENDLED),ID_INDICATOR_SENDLED,SBPS_NOBORDERS,10);
  345.     }
  346.  
  347.     if (m_wndSplitter.GetSafeHwnd())
  348.     {
  349.         //Hide the queue if visible
  350.         m_wndSplitter.HideRow(1, 0);
  351.     }
  352.     //Now only the main splitter gets resized
  353.     CFrameWnd::OnSize(nType, cx, cy);
  354.     if (m_wndSplitter.GetSafeHwnd())
  355.     {
  356.         //Restore the queue
  357.         m_wndSplitter.ShowRow(1);
  358.     }
  359.     
  360.     if (m_wndStatusBar.GetSafeHwnd())
  361.     {
  362.         RECT rc;
  363.  
  364.         m_wndStatusBar.GetItemRect(m_wndStatusBar.CommandToIndex(ID_INDICATOR_RECVLED), &rc);
  365.  
  366.         // Reposition the first LED correctly!
  367.         m_RecvLed.SetWindowPos(&wndTop, rc.left, rc.top+1, rc.right - rc.left,
  368.             rc.bottom - rc.top, 0); 
  369.  
  370.         m_wndStatusBar.GetItemRect(m_wndStatusBar.CommandToIndex(ID_INDICATOR_SENDLED), &rc);
  371.     
  372.         // Reposition the second LED correctly!
  373.         m_SendLed.SetWindowPos(&wndTop, rc.left, rc.top+1, rc.right - rc.left,
  374.                 rc.bottom - rc.top, 0); 
  375.     }
  376. }
  377.  
  378. void CMainFrame::OnClose() 
  379. {
  380.     CFrameWnd::OnClose();
  381. }
  382.  
  383. void CMainFrame::OnEditSettings() 
  384. {
  385.     if (m_nEdit)
  386.     {
  387.         MessageBeep(MB_OK);
  388.         return;
  389.     }
  390.     m_nEdit |= 0x04;
  391.     SendCommand(5, 0, 0);
  392.     ShowStatus(_T("Retrieving settings, please wait..."), 0);
  393. }
  394.  
  395. void CMainFrame::OnActive() 
  396. {
  397.     if (m_nServerState & STATE_ONLINE && !(m_nServerState & STATE_MASK_GOOFFLINE))
  398.     {
  399.         if (!GetUsersPane()->m_pListCtrl->GetItemCount())
  400.         {
  401.             if (AfxMessageBox(_T("Do you really want to take the server offline?"), MB_YESNO | MB_ICONQUESTION) != IDYES)
  402.                 return;
  403.             int nServerState = m_nServerState | STATE_GOOFFLINE_NOW;
  404.             unsigned char buffer[2];
  405.             buffer[0] = nServerState / 256;
  406.             buffer[1] = nServerState % 256;
  407.             SendCommand(2, buffer, 2);
  408.             ShowStatus(_T("Server is going offline..."), 0);
  409.             return;
  410.         }
  411.         else
  412.         {
  413.             COfflineAskDlg dlg;
  414.             if (dlg.DoModal() != IDOK)
  415.                 return;
  416.             if (dlg.m_nRadio == 2)
  417.             {
  418.                 int nServerState = m_nServerState | STATE_GOOFFLINE_WAITTRANSFER;
  419.                 unsigned char buffer[2];
  420.                 buffer[0] = nServerState / 256;
  421.                 buffer[1] = nServerState % 256;
  422.                 SendCommand(2, buffer, 2);
  423.                 ShowStatus(_T("Server is going offline..."), 0);
  424.                 return;
  425.             }
  426.             if (dlg.m_nRadio == 1)
  427.             {
  428.                 int nServerState = m_nServerState | STATE_GOOFFLINE_LOGOUT;
  429.                 unsigned char buffer[2];
  430.                 buffer[0] = nServerState / 256;
  431.                 buffer[1] = nServerState % 256;
  432.                 SendCommand(2, buffer, 2);
  433.                 ShowStatus(_T("Server is going offline..."), 0);                
  434.                 return;
  435.             }                
  436.             else
  437.             {
  438.                 int nServerState = m_nServerState | STATE_GOOFFLINE_NOW;
  439.                 unsigned char buffer[2];
  440.                 buffer[0] = nServerState / 256;
  441.                 buffer[1] = nServerState % 256;
  442.                 SendCommand(2, buffer, 2);
  443.                 ShowStatus(_T("Server is going offline..."), 0);
  444.                 return;
  445.             }
  446.         }
  447.     }
  448.     else
  449.     {
  450.         int nServerState = 1 + (m_nServerState & STATE_LOCKED);
  451.         unsigned char buffer[2];
  452.         buffer[0] = nServerState / 256;
  453.         buffer[1] = nServerState % 256;
  454.         SendCommand(2, buffer, 2);
  455.     }
  456. }
  457.  
  458. void CMainFrame::OnUpdateActive(CCmdUI* pCmdUI) 
  459. {
  460.     pCmdUI->Enable(m_pAdminSocket && m_pAdminSocket->IsConnected());
  461.     pCmdUI->SetCheck(m_nServerState & STATE_ONLINE && !(m_nServerState & STATE_MASK_GOOFFLINE));
  462. }
  463.  
  464. void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
  465. {
  466.     CFrameWnd::OnSysCommand(nID, lParam);
  467.     if (nID == SC_MINIMIZE)
  468.         ShowWindow(SW_HIDE);
  469.     else if (nID == SC_RESTORE)
  470.         ShowWindow(SW_SHOW);
  471. }
  472.  
  473. //// SetupTrayIcon /////////////////////////////////////////////////////
  474. // If we're minimized, create an icon in the systray.  Otherwise, remove
  475. // the icon, if one is present.
  476.  
  477. void CMainFrame::SetupTrayIcon()
  478. {
  479.     m_TrayIcon.Create(0, nTrayNotificationMsg_, _T("FileZilla Server"),
  480.         0, IDR_SYSTRAY_MENU);
  481.     m_TrayIcon.SetIcon(IDI_UNKNOWN);
  482. }
  483.  
  484.  
  485. //// SetupTaskBarButton ////////////////////////////////////////////////
  486. // Show or hide the taskbar button for this app, depending on whether
  487. // we're minimized right now or not.
  488.  
  489. void CMainFrame::OnTrayExit() 
  490. {
  491.     if (!m_bQuit)
  492.         OnClose();    
  493. }
  494.  
  495. void CMainFrame::OnTrayRestore() 
  496. {
  497.     ShowWindow(SW_RESTORE);
  498.     ShowWindow(SW_SHOW);
  499. }
  500.  
  501. void CMainFrame::SetIcon()
  502. {
  503.     if (!m_pAdminSocket || !m_pAdminSocket->IsConnected())
  504.     {
  505.         m_TrayIcon.StopAnimation();
  506.         m_TrayIcon.SetIcon(IDI_UNKNOWN);
  507.     }
  508.     else if (!(m_nServerState & STATE_ONLINE) || m_nServerState & STATE_MASK_GOOFFLINE)
  509.     {
  510.         if (!GetUsersPane()->m_pListCtrl->GetItemCount())
  511.         {    
  512.             m_TrayIcon.StopAnimation();
  513.             m_TrayIcon.SetIcon(IDI_RED);
  514.         }
  515.         else
  516.         {
  517.             m_TrayIcon.SetIconList(IDI_GREEN, IDI_RED);
  518.             m_TrayIcon.Animate(500);
  519.         }
  520.     }
  521.     else if (m_nServerState & STATE_LOCKED)
  522.     {
  523.         if (GetUsersPane()->m_pListCtrl->GetItemCount())
  524.         {    
  525.             m_TrayIcon.SetIconList(IDI_GREEN, IDI_YELLOW);
  526.             m_TrayIcon.Animate(300);
  527.         }
  528.         else
  529.         {
  530.             m_TrayIcon.SetIconList(IDI_YELLOW, IDI_RED);
  531.             m_TrayIcon.Animate(500);
  532.         }
  533.  
  534.     }
  535.     else
  536.     {
  537.         m_TrayIcon.StopAnimation();
  538.         m_TrayIcon.SetIcon(GetUsersPane()->m_pListCtrl->GetItemCount()?IDI_GREEN:IDI_YELLOW);
  539.     }
  540.  
  541. }
  542.  
  543. void CMainFrame::OnLock() 
  544. {
  545.     if (!(m_nServerState & STATE_ONLINE) || m_nServerState & STATE_MASK_GOOFFLINE)
  546.         return;
  547.     if (m_nServerState & STATE_LOCKED)
  548.     {
  549.         int nServerState = m_nServerState & ~STATE_LOCKED;
  550.         unsigned char buffer[2];
  551.         buffer[0] = nServerState / 256;
  552.         buffer[1] = nServerState % 256;
  553.         SendCommand(2, buffer, 2);
  554.     }
  555.     else
  556.     {
  557.         if (AfxMessageBox(_T("Do you really want to lock the server? No new connenctions will be accepted while locked."), MB_YESNO|MB_ICONQUESTION)!=IDYES)
  558.             return;
  559.         int nServerState = m_nServerState | STATE_LOCKED;
  560.         unsigned char buffer[2];
  561.         buffer[0] = nServerState / 256;
  562.         buffer[1] = nServerState % 256;
  563.         SendCommand(2, buffer, 2);
  564.         ShowStatus("Server locked", 0);    
  565.     }    
  566. }
  567.  
  568. void CMainFrame::OnUpdateLock(CCmdUI* pCmdUI) 
  569. {
  570.     pCmdUI->Enable(m_pAdminSocket && m_pAdminSocket->IsConnected() && m_nServerState & STATE_ONLINE && !(m_nServerState & STATE_MASK_GOOFFLINE));
  571.     pCmdUI->SetCheck((m_nServerState & STATE_LOCKED) ? 1 : 0);
  572.     
  573. }
  574.  
  575. CString FormatSpeed(__int64 diff, const int span)
  576. {
  577.     diff = (__int64)((double)diff * 1000 / span);
  578.  
  579.     CString str = _T("");
  580.     CString digit;
  581.  
  582.     int shift = 0;
  583.     while (diff >= 10000)
  584.     {
  585.         diff /= 10;
  586.         shift++;
  587.     }
  588.  
  589.     while (diff)
  590.     {
  591.         digit = (TCHAR)('0' + static_cast<TCHAR>(diff % 10));
  592.         str = digit + str;
  593.         diff /= 10;
  594.     }
  595.  
  596.     if (str == _T(""))
  597.         str = _T("0");
  598.  
  599.     if (shift % 3)
  600.         str = str.Left((shift % 3) + 1) + "," + str.Right(str.GetLength() - (shift % 3) - 1);
  601.     shift += 2;
  602.     shift /= 3;
  603.     if (!shift)
  604.         str += _T(" B/s");
  605.     else if (shift == 1)
  606.         str += _T(" KB/s");
  607.     else if (shift == 2)
  608.         str += _T(" MB/s");
  609.     else if (shift == 3)
  610.         str += _T(" GB/s");
  611.     else if (shift == 4)
  612.         str += _T(" TB/s");
  613.     else
  614.         str = _T("n/a"); //If this happens, you really have a fast connection
  615.     return str;
  616. }
  617.  
  618. void CMainFrame::OnTimer(UINT_PTR nIDEvent) 
  619. {
  620.     if (!nIDEvent)
  621.         return;
  622.     else if (nIDEvent == m_nTimerID)
  623.     {
  624.         SendCommand(8);
  625.         m_TrayIcon.RefreshIcon();
  626.     }
  627.     else if (nIDEvent == m_nRateTimerID)
  628.     {
  629.         const int span = GetTickCount() - m_lastchecktime;
  630.         m_lastchecktime=GetTickCount();
  631.  
  632.         __int64 diff = m_nSendCount - m_nOldSendCount;
  633.         m_nOldSendCount = m_nSendCount;
  634.  
  635.         if (m_lastwritediff && diff)
  636.         {
  637.             __int64 tmp = diff;
  638.             diff = (diff + m_lastwritediff) / 2;
  639.             m_lastwritediff = tmp;
  640.         }
  641.         else
  642.             m_lastwritediff = diff;
  643.  
  644.         CString writeSpeed = FormatSpeed(diff, span);        
  645.         SetStatusbarText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_SENDRATE), writeSpeed);
  646.         
  647.         diff = m_nRecvCount - m_nOldRecvCount;
  648.         m_nOldRecvCount = m_nRecvCount;
  649.  
  650.         if (m_lastreaddiff && diff)
  651.         {
  652.             __int64 tmp = diff;
  653.             diff = (diff + m_lastreaddiff) / 2;
  654.             m_lastreaddiff = tmp;
  655.         }
  656.         else
  657.             m_lastreaddiff = diff;
  658.         
  659.         CString readSpeed = FormatSpeed(diff, span);        
  660.         SetStatusbarText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_RECVRATE), readSpeed);
  661.     }
  662.     else if (nIDEvent == m_nReconnectTimerID)
  663.     {
  664.         KillTimer(m_nReconnectTimerID);
  665.         m_nReconnectTimerID = 0;
  666.         
  667.         if (m_pAdminSocket)
  668.             return;
  669.  
  670.         m_pAdminSocket = new CAdminSocket(this);
  671.         ShowStatus("Reconnecting to server...", 0);
  672.         DoConnect();
  673.     }
  674.  
  675.     CFrameWnd::OnTimer(nIDEvent);
  676. }
  677.  
  678. void CMainFrame::SetStatusbarText(int nIndex,CString str)
  679. {
  680.     m_wndStatusBar.GetStatusBarCtrl().SetText(str,nIndex,0);
  681.     HFONT hFont = (HFONT)m_wndStatusBar.SendMessage(WM_GETFONT);
  682.     if (str=="")
  683.     {
  684.         str.LoadString(m_wndStatusBar.GetItemID(nIndex));        
  685.     }
  686.     CClientDC dcScreen(NULL);
  687.     HGDIOBJ hOldFont = NULL;
  688.     if (hFont != NULL)
  689.     hOldFont = dcScreen.SelectObject(hFont);
  690.     int cx=dcScreen.GetTextExtent(str).cx;
  691.     int cxold;
  692.     unsigned int nID,nStyle;
  693.     m_wndStatusBar.GetPaneInfo(nIndex,nID,nStyle,cxold);
  694.     if (cx!=cxold)
  695.     {
  696.         if (cx<cxold)
  697.             cx=cxold;
  698.         m_wndStatusBar.SetPaneInfo(nIndex,nID,nStyle,cx);
  699.     
  700.     }
  701.     if (hOldFont != NULL)
  702.         dcScreen.SelectObject(hOldFont);
  703.  
  704. }
  705.  
  706. void CMainFrame::OnMenuEditUsers() 
  707. {
  708.     if (m_nEdit)
  709.     {
  710.         MessageBeep(MB_OK);
  711.         return;
  712.     }
  713.     m_nEdit |= 1;
  714.     SendCommand(6, 0, 0);
  715.     ShowStatus(_T("Retrieving account settings, please wait..."), 0);
  716. }
  717.  
  718. void CMainFrame::OnMenuEditGroups() 
  719. {
  720.     if (m_nEdit)
  721.     {
  722.         MessageBeep(MB_OK);
  723.         return;
  724.     }
  725.     m_nEdit |= 2;
  726.     SendCommand(6, 0, 0);
  727.     ShowStatus(_T("Retrieving account settings, please wait..."), 0);
  728. }
  729.  
  730. void CMainFrame::ShowStatus(const CString& status, int nType)
  731. {
  732.     CStatusView *view = GetStatusPane();
  733.     view->ShowStatus(status, nType);
  734. }
  735.  
  736. void CMainFrame::ShowStatusRaw(const char *status, int nType)
  737. {
  738. #ifdef _UNICODE
  739.     CString msg(ConvFromNetwork(status));
  740. #else
  741.     CString msg(ConvToLocal(ConvFromNetwork(status)));
  742. #endif
  743.     ShowStatus(msg, nType);
  744. }
  745.  
  746. void CMainFrame::ParseReply(int nReplyID, unsigned char *pData, int nDataLength)
  747. {
  748.     switch(nReplyID)
  749.     {
  750.     case 0:
  751.         {
  752.             ShowStatus(_T("Logged on"), 0);
  753.             SendCommand(2);
  754.             unsigned char buffer = USERCONTROL_GETLIST;
  755.             SendCommand(3, &buffer, 1);
  756.         }
  757.         break;
  758.     case 1:
  759.         {
  760.             char *pBuffer = new char[nDataLength];
  761.             memcpy(pBuffer, pData+1, nDataLength-1);
  762.             pBuffer[nDataLength-1] = 0;
  763.             ShowStatusRaw(pBuffer, *pData);
  764.             delete [] pBuffer;
  765.         }
  766.         break;
  767.     case 2:
  768.         m_nServerState = *pData*256 + pData[1];
  769.         SetIcon();
  770.         break;
  771.     case 3:
  772.         {
  773.             if (nDataLength<2)
  774.             {
  775.                 ShowStatus(_T("Protocol error: Unexpected data length"), 1);
  776.                 return;
  777.             }
  778.             else if (!GetUsersPane()->m_pListCtrl->ParseUserControlCommand(pData, nDataLength))
  779.                 ShowStatus(_T("Protocol error: Invalid data"), 1);
  780.         }
  781.         break;
  782.     case 5:
  783.         if (nDataLength == 1)
  784.         {
  785.             if (*pData == 0)
  786.                 ShowStatus(_T("Done sending settings."), 0);
  787.             else if (*pData == 1)
  788.                 ShowStatus(_T("Could not change settings"), 1);
  789.             break;
  790.         }
  791.         ShowStatus(_T("Done retrieving settings"), 0);
  792.         if (nDataLength<2)
  793.             ShowStatus(_T("Protocol error: Unexpected data length"), 1);
  794.         else
  795.         {
  796.             if ((m_nEdit & 0x1C) == 0x04)
  797.             {
  798.                 m_pOptionsDlg = new COptionsDlg(m_pOptions, m_pAdminSocket->IsLocal());
  799.                 m_nEdit |= 0x08;
  800.                 if (!m_pOptionsDlg->Init(pData, nDataLength))
  801.                 {
  802.                     ShowStatus(_T("Protocol error: Invalid data"), 1);
  803.                     delete m_pOptionsDlg;
  804.                     m_pOptionsDlg = 0;
  805.                     m_nEdit = 0;
  806.                 }
  807.                 else if (!PostMessage(WM_APP))
  808.                 {
  809.                     ShowStatus(_T("Can't send window message"), 1);
  810.                     delete m_pOptionsDlg;
  811.                     m_pOptionsDlg = 0;
  812.                     m_nEdit = 0;
  813.                 }
  814.             }
  815.         }
  816.         break;
  817.     case 6:
  818.         if (nDataLength == 1)
  819.         {
  820.             if (*pData == 0)
  821.                 ShowStatus(_T("Done sending account settings."), 0);
  822.             else if (*pData == 1)
  823.                 ShowStatus(_T("Could not change account settings"), 1);
  824.             break;
  825.         }
  826.         ShowStatus(_T("Done retrieving account settings"), 0);
  827.         if (nDataLength<2)
  828.             ShowStatus(_T("Protocol error: Unexpected data length"), 1);
  829.         else
  830.         {
  831.             if ((m_nEdit & 0x19) == 0x01)
  832.             {
  833.                 m_pUsersDlg = new CUsersDlg(this, m_pAdminSocket->IsLocal());
  834.                 m_nEdit |= 0x08;
  835.                 if (!m_pUsersDlg->Init(pData, nDataLength))
  836.                 {
  837.                     ShowStatus(_T("Protocol error: Invalid data"), 1);
  838.                     delete m_pUsersDlg;
  839.                     m_pUsersDlg = 0;
  840.                     m_nEdit = 0;
  841.                     break;
  842.                 }
  843.                 else if (!PostMessage(WM_APP))
  844.                 {
  845.                     ShowStatus(_T("Can't send window message"), 1);
  846.                     delete m_pUsersDlg;
  847.                     m_pUsersDlg = 0;
  848.                     m_nEdit = 0;
  849.                 }
  850.             }
  851.             if ((m_nEdit & 0x1A) == 0x02)
  852.             {
  853.                 m_pGroupsDlg = new CGroupsDlg(this, m_pAdminSocket->IsLocal());
  854.                 m_nEdit |= 0x08;
  855.                 if (!m_pGroupsDlg->Init(pData, nDataLength))
  856.                 {
  857.                     ShowStatus(_T("Protocol error: Invalid data"), 1);
  858.                     delete m_pGroupsDlg;
  859.                     m_pGroupsDlg = 0;
  860.                     m_nEdit = 0;
  861.                     break;
  862.                 }
  863.                 else if (!PostMessage(WM_APP))
  864.                 {
  865.                     ShowStatus(_T("Can't send window message"), 1);
  866.                     delete m_pGroupsDlg;
  867.                     m_pGroupsDlg = 0;
  868.                     m_nEdit = 0;
  869.                 }
  870.             }
  871.         }
  872.         break;
  873.     case 8:
  874.         break;
  875.     default:
  876.         {
  877.             CString str;
  878.             str.Format(_T("Protocol error: Unexpected reply id (%d)."), nReplyID);
  879.             ShowStatus(str, 1);
  880.             break;
  881.         }
  882.     }
  883. }
  884.  
  885. void CMainFrame::ParseStatus(int nStatusID, unsigned char *pData, int nDataLength)
  886. {
  887.     switch(nStatusID)
  888.     {
  889.     case 1:
  890.         {
  891.             char *pBuffer = new char[nDataLength];
  892.             memcpy(pBuffer, pData+1, nDataLength-1);
  893.             pBuffer[nDataLength-1] = 0;
  894.             ShowStatusRaw(pBuffer, *pData);
  895.             delete [] pBuffer;
  896.         }
  897.         break;
  898.     case 2:
  899.         m_nServerState = *pData*256 + pData[1];
  900.         SetIcon();
  901.         break;
  902.     case 3:
  903.         {
  904.             if (nDataLength<2)
  905.             {
  906.                 ShowStatus(_T("Protocol error: Unexpected data length"), 1);
  907.                 return;
  908.             }
  909.             else if (!GetUsersPane()->m_pListCtrl->ParseUserControlCommand(pData, nDataLength))
  910.                 ShowStatus(_T("Protocol error: Invalid data"), 1);
  911.         }
  912.         break;
  913.     case 4:
  914.         {
  915.             if (nDataLength < 10)
  916.             {
  917.                 ShowStatus(_T("Protocol error: Unexpected data length"), 1);
  918.                 return;
  919.             }
  920.             
  921.             CString msg;
  922.  
  923.             char *buffer = new char[nDataLength - 9 + 1];
  924.             unsigned char *p = pData + 9;
  925.             char *q = buffer;
  926.             int pos = 0;
  927.             while ((pos + 9) < nDataLength)
  928.             {
  929.                 if (*p == '-')
  930.                 {
  931.                     *q = 0;
  932.                     msg = ConvFromNetwork(buffer);
  933.                     q = buffer;
  934.                     
  935.                     DWORD timeHigh = GET32(pData + 1);
  936.                     DWORD timeLow = GET32(pData + 5);
  937.  
  938.                     FILETIME fFileTime;
  939.                     fFileTime.dwHighDateTime = timeHigh;
  940.                     fFileTime.dwLowDateTime = timeLow;
  941.  
  942.                     SYSTEMTIME sFileTime;
  943.                     FileTimeToSystemTime(&fFileTime, &sFileTime);
  944.                     
  945.                     TCHAR datetime[200];
  946.                     int res = GetDateFormat(
  947.                             LOCALE_USER_DEFAULT,    // locale for which date is to be formatted
  948.                             DATE_SHORTDATE,            // flags specifying function options
  949.                             &sFileTime,                // date to be formatted
  950.                             0,                        // date format string
  951.                             datetime,                // buffer for storing formatted string
  952.                             200                        // size of buffer
  953.                         );
  954.  
  955.                     if (res)
  956.                     {
  957.                         msg += datetime;
  958.                         msg += ' ';
  959.                     }
  960.     
  961.                     res = GetTimeFormat(
  962.                             LOCALE_USER_DEFAULT,    // locale for which date is to be formatted
  963.                             TIME_FORCE24HOURFORMAT,    // flags specifying function options
  964.                             &sFileTime,                // date to be formatted
  965.                             0,                        // date format string
  966.                             datetime,                // buffer for storing formatted string
  967.                             200                        // size of buffer
  968.                         );
  969.  
  970.                     if (res)
  971.                     {
  972.                         msg += datetime;
  973.                         msg += ' ';
  974.                     }
  975.  
  976.                     if ((nDataLength - pos - 9) > 0)
  977.                     {
  978.                         memcpy(q, p, nDataLength - pos - 9);
  979.                         q += nDataLength - pos - 9;
  980.                     }
  981.                     break;
  982.                 }
  983.                 else
  984.                     *(q++) = *(p++);
  985.  
  986.                 pos++;
  987.             }
  988.             *q = 0;
  989.             if (q != buffer)
  990.                 msg += ConvFromNetwork(buffer);
  991.             if (msg != _T(""))
  992.                 ShowStatus(msg, *pData);
  993.             delete [] buffer;
  994.         }
  995.         break;
  996.     case 7:
  997.         if (nDataLength != 5)
  998.             ShowStatus(_T("Protocol error: Invalid data"), 1);
  999.         else
  1000.         {
  1001.             int nType = *pData;
  1002.             int size = (int)GET32(pData + 1);
  1003.             
  1004.             if (!nType)
  1005.             {
  1006.                 m_nRecvCount += size;
  1007.                 m_RecvLed.Ping(100);
  1008.                 CString str;
  1009.                 str.Format(_T("%s bytes received"), makeUserFriendlyString(m_nRecvCount).GetString());
  1010.                 SetStatusbarText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_RECVCOUNT), str);
  1011.             }
  1012.             else
  1013.             {
  1014.                 m_nSendCount += size;
  1015.                 m_SendLed.Ping(100);
  1016.                 CString str;
  1017.                 str.Format(_T("%s bytes sent"), makeUserFriendlyString(m_nSendCount).GetString());
  1018.                 SetStatusbarText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_SENDCOUNT), str);
  1019.             }
  1020.  
  1021.         }
  1022.         break;
  1023.     default:
  1024.         {
  1025.             CString str;
  1026.             str.Format(_T("Protocol error: Unexpected status id (%d)."), nStatusID);
  1027.             ShowStatus(str, 1);
  1028.         }
  1029.         break;
  1030.  
  1031.     }
  1032. }
  1033.  
  1034. BOOL CMainFrame::SendCommand(int nType)
  1035. {
  1036.     if (!m_pAdminSocket)
  1037.         return FALSE;
  1038.     if (!m_pAdminSocket->SendCommand(nType))
  1039.     {
  1040.         CloseAdminSocket();
  1041.         ShowStatus("Error: Connection to server lost...", 1);
  1042.         return FALSE;
  1043.     }
  1044.     return TRUE;
  1045. }
  1046.  
  1047. BOOL CMainFrame::SendCommand(int nType, void *pData, int nDataLength)
  1048. {
  1049.     if (!m_pAdminSocket)
  1050.         return FALSE;
  1051.     if (!m_pAdminSocket->SendCommand(nType, pData, nDataLength))
  1052.     {
  1053.         CloseAdminSocket();
  1054.         ShowStatus("Error: Connection to server lost...", 1);
  1055.         return FALSE;
  1056.     }
  1057.     return TRUE;
  1058. }
  1059.  
  1060. void CMainFrame::CloseAdminSocket(bool shouldReconnect /*=true*/)
  1061. {
  1062.     if (m_pAdminSocket)
  1063.     {
  1064.         m_pAdminSocket->DoClose();
  1065.         delete m_pAdminSocket;
  1066.         m_pAdminSocket = NULL;
  1067.         SetIcon();
  1068.  
  1069.         CString title;
  1070.         title.LoadString(IDR_MAINFRAME);
  1071.         SetWindowText(title + _T(" (disconnected)"));
  1072.     }
  1073.     m_nEdit = 0;
  1074.  
  1075.     if (!shouldReconnect)
  1076.     {
  1077.         if (m_nReconnectTimerID)
  1078.         {
  1079.             KillTimer(m_nReconnectTimerID);
  1080.             m_nReconnectTimerID = 0;
  1081.         }
  1082.     }
  1083.     else
  1084.     {
  1085.         if (!m_nReconnectTimerID)
  1086.         {
  1087.             m_nReconnectCount++;
  1088.             if (m_nReconnectCount < 15)
  1089.             {
  1090.                 ShowStatus("Trying to reconnect in 5 seconds", 0);
  1091.                 m_nReconnectTimerID = SetTimer(7779, 5000, 0);
  1092.             }
  1093.             else
  1094.                 m_nReconnectCount = 0;
  1095.         }
  1096.     }
  1097. }
  1098.  
  1099. void CMainFrame::OnFileConnect() 
  1100. {
  1101.     if (m_nReconnectTimerID)
  1102.     {
  1103.         KillTimer(m_nReconnectTimerID);
  1104.         m_nReconnectTimerID = 0;
  1105.     }
  1106.     if (m_pAdminSocket)
  1107.         if (AfxMessageBox(_T("Do you really want to close the current connection?"), MB_ICONQUESTION|MB_YESNO) != IDYES)
  1108.             return;
  1109.     CConnectDialog dlg(m_pOptions);
  1110.     if (dlg.DoModal() == IDOK)
  1111.     {
  1112.         CloseAdminSocket(false);
  1113.         m_pAdminSocket = new CAdminSocket(this);
  1114.         ShowStatus("Connecting to server...", 0);
  1115.         DoConnect();
  1116.     }
  1117. }
  1118.  
  1119. void CMainFrame::OnFileDisconnect() 
  1120. {
  1121.     CloseAdminSocket(false);
  1122. }
  1123.  
  1124. void CMainFrame::OnUpdateFileDisconnect(CCmdUI* pCmdUI) 
  1125. {
  1126.     pCmdUI->Enable(m_pAdminSocket?TRUE:FALSE);    
  1127. }
  1128.  
  1129. void CMainFrame::OnUpdateEditSettings(CCmdUI* pCmdUI) 
  1130. {
  1131.     pCmdUI->Enable(m_pAdminSocket && m_pAdminSocket->IsConnected());
  1132. }
  1133.  
  1134. void CMainFrame::OnUpdateMenuEditUsers(CCmdUI* pCmdUI) 
  1135. {
  1136.     pCmdUI->Enable(m_pAdminSocket && m_pAdminSocket->IsConnected());
  1137. }
  1138.  
  1139. void CMainFrame::OnUpdateMenuEditGroups(CCmdUI* pCmdUI) 
  1140. {
  1141.     pCmdUI->Enable(m_pAdminSocket && m_pAdminSocket->IsConnected());
  1142. }
  1143.  
  1144. void CMainFrame::OnUpdateUsers(CCmdUI* pCmdUI) 
  1145. {
  1146.     pCmdUI->Enable(m_pAdminSocket && m_pAdminSocket->IsConnected());
  1147. }
  1148.  
  1149. void CMainFrame::OnUpdateGroups(CCmdUI* pCmdUI) 
  1150. {
  1151.     pCmdUI->Enable(m_pAdminSocket && m_pAdminSocket->IsConnected());
  1152. }
  1153.  
  1154. // this function gets called whenever the user decides to quit the app
  1155. // (by pressing the 'x' for example
  1156. void CMainFrame::OnDestroy() 
  1157. {
  1158.     m_pOptions->SetOption(IOPTION_USERSORTING, GetUsersPane()->m_pListCtrl->GetSortColumn() + (GetUsersPane()->m_pListCtrl->GetSortDirection() << 4));
  1159.     CloseAdminSocket(false);
  1160.     CFrameWnd::OnDestroy();
  1161. }
  1162.  
  1163. BOOL CMainFrame::OnEraseBkgnd(CDC* pDC)
  1164. {
  1165.     return TRUE;
  1166. }
  1167.  
  1168. LRESULT CMainFrame::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  1169. {
  1170.     if (message == WM_APP)
  1171.     {
  1172.         if ((m_nEdit & 0x1C) == 0x0C)
  1173.         {
  1174.             m_nEdit |= 0x10;
  1175.             if (m_pOptionsDlg->Show())
  1176.             {
  1177.                 char *pBuffer;
  1178.                 DWORD dwBufferLength;
  1179.                 if (m_pOptionsDlg->GetAsCommand(&pBuffer, &dwBufferLength))
  1180.                 {
  1181.                     SendCommand(5, pBuffer, dwBufferLength);
  1182.                     ShowStatus(_T("Sending settings, please wait..."), 0);
  1183.                     delete [] pBuffer;
  1184.                 }
  1185.                 else
  1186.                     ShowStatus(_T("Could not serialize settings, too much data."), 1);
  1187.             }
  1188.             delete m_pOptionsDlg;
  1189.             m_pOptionsDlg = 0;
  1190.             m_nEdit = 0;
  1191.         }
  1192.         else if ((m_nEdit & 0x19) == 0x09)
  1193.         {
  1194.             m_nEdit |= 0x10;
  1195.             if (m_pUsersDlg->DoModal() == IDOK)
  1196.             {
  1197.                 char *pBuffer;
  1198.                 DWORD dwBufferLength;
  1199.                 if (m_pUsersDlg->GetAsCommand(&pBuffer, &dwBufferLength))
  1200.                 {
  1201.                     SendCommand(6, pBuffer, dwBufferLength);
  1202.                     ShowStatus(_T("Sending account settings, please wait..."), 0);
  1203.                     delete [] pBuffer;
  1204.                 }
  1205.             }
  1206.             delete m_pUsersDlg;
  1207.             m_pUsersDlg = 0;
  1208.             m_nEdit = 0;
  1209.         }
  1210.         else if ((m_nEdit & 0x1A) == 0x0A)
  1211.         {
  1212.             m_nEdit |= 0x10;
  1213.             if (m_pGroupsDlg->DoModal() == IDOK)
  1214.             {
  1215.                 char *pBuffer;
  1216.                 DWORD dwBufferLength;
  1217.                 if (m_pGroupsDlg->GetAsCommand(&pBuffer, &dwBufferLength))
  1218.                 {
  1219.                     SendCommand(6, pBuffer, dwBufferLength);
  1220.                     ShowStatus(_T("Sending account settings, please wait..."), 0);
  1221.                     delete [] pBuffer;
  1222.                 }
  1223.             }
  1224.             delete m_pGroupsDlg;
  1225.             m_pGroupsDlg = 0;
  1226.             m_nEdit = 0;
  1227.         }
  1228.     }
  1229.     else if (message == WM_APP + 1)
  1230.         OnAdminInterfaceClosed();
  1231.     
  1232.     return CFrameWnd::DefWindowProc(message, wParam, lParam);
  1233. }
  1234.  
  1235. void CMainFrame::OnAdminInterfaceConnected()
  1236. {
  1237.     m_nReconnectCount = 0;
  1238.  
  1239.     CString title;
  1240.     title.LoadString(IDR_MAINFRAME);
  1241.     CString ip;
  1242.     UINT port;
  1243.     if (m_pAdminSocket->GetPeerName(ip, port))
  1244.     {
  1245.         if (ip.Find(':') != -1)
  1246.             ip = GetIPV6ShortForm(ip);
  1247.         SetWindowText(title + _T(" (") + ip + _T(")"));
  1248.     }
  1249. }
  1250.  
  1251. void CMainFrame::OnAdminInterfaceClosed()
  1252. {
  1253.     if (m_pAdminSocket && m_pAdminSocket->IsClosed())
  1254.     {
  1255.         CloseAdminSocket();
  1256.     }
  1257. }
  1258.  
  1259. void CMainFrame::OnDisplayLogicalNames()
  1260. {
  1261.     GetUsersPane()->m_pListCtrl->SetDisplayPhysicalNames(false);
  1262.     m_pOptions->SetOption(IOPTION_FILENAMEDISPLAY, 0);
  1263. }
  1264.  
  1265. void CMainFrame::OnDisplayPhysicalNames()
  1266. {
  1267.     GetUsersPane()->m_pListCtrl->SetDisplayPhysicalNames(true);
  1268.     m_pOptions->SetOption(IOPTION_FILENAMEDISPLAY, 1);
  1269. }
  1270.  
  1271. void CMainFrame::OnUpdateDisplayLogicalNames(CCmdUI* pCmdUI)
  1272. {
  1273.     pCmdUI->SetRadio(!GetUsersPane()->m_pListCtrl->GetDisplayPhysicalNames());
  1274. }
  1275.  
  1276. void CMainFrame::OnUpdateDisplayPhysicalNames(CCmdUI* pCmdUI)
  1277. {
  1278.     pCmdUI->SetRadio(GetUsersPane()->m_pListCtrl->GetDisplayPhysicalNames());
  1279. }
  1280.  
  1281. void CMainFrame::OnDisplaySortMenu()
  1282. {
  1283.     // load and display popup menu
  1284.     CMenu menu;
  1285.     menu.LoadMenu(IDR_SORTMENU);
  1286.     CMenu* pPopup = menu.GetSubMenu(0);
  1287.     ASSERT(pPopup);
  1288.  
  1289.     CRect rc;
  1290.     m_wndUserListToolBar.GetItemRect(m_wndUserListToolBar.CommandToIndex(ID_USERLISTTOOLBAR_SORT), &rc);
  1291.     m_wndUserListToolBar.ClientToScreen(&rc);
  1292.  
  1293.     pPopup->TrackPopupMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL,
  1294.         rc.left, rc.bottom, this, &rc);
  1295. }
  1296.  
  1297. void CMainFrame::OnToolbarDropDown(NMHDR* pnmh, LRESULT* plRes)
  1298. {
  1299.     NMTOOLBAR* pnmtb = (NMTOOLBAR*)pnmh;
  1300.     if (pnmtb->iItem == ID_USERLISTTOOLBAR_SORT)
  1301.         OnDisplaySortMenu();
  1302. }
  1303.  
  1304. void CMainFrame::OnDisplaySortByUserid()
  1305. {
  1306.     GetUsersPane()->m_pListCtrl->SetSortColumn(0);
  1307. }
  1308.  
  1309. void CMainFrame::OnDisplaySortByAccount()
  1310. {
  1311.     GetUsersPane()->m_pListCtrl->SetSortColumn(1);
  1312. }
  1313.  
  1314. void CMainFrame::OnDisplaySortByIP()
  1315. {
  1316.     GetUsersPane()->m_pListCtrl->SetSortColumn(2);
  1317. }
  1318.  
  1319. void CMainFrame::OnUpdateDisplaySortByUserid(CCmdUI* pCmdUI)
  1320. {
  1321.     pCmdUI->SetRadio(GetUsersPane()->m_pListCtrl->GetSortColumn() == 0);
  1322. }
  1323.  
  1324. void CMainFrame::OnUpdateDisplaySortByAccount(CCmdUI* pCmdUI)
  1325. {
  1326.     pCmdUI->SetRadio(GetUsersPane()->m_pListCtrl->GetSortColumn() == 1);
  1327. }
  1328.  
  1329. void CMainFrame::OnUpdateDisplaySortByIP(CCmdUI* pCmdUI)
  1330. {
  1331.     pCmdUI->SetRadio(GetUsersPane()->m_pListCtrl->GetSortColumn() == 3);
  1332. }
  1333.  
  1334. void CMainFrame::DoConnect()
  1335. {
  1336.     int family;
  1337.     if (!GetIPV6LongForm(m_pOptions->GetOption(IOPTION_LASTSERVERADDRESS)).IsEmpty())
  1338.         family = AF_INET6;
  1339.     else
  1340.         family = AF_INET;
  1341.  
  1342.     m_pAdminSocket->Create(0, SOCK_STREAM, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, 0, family);
  1343.  
  1344.     m_pAdminSocket->m_Password = m_pOptions->GetOption(IOPTION_LASTSERVERPASS);
  1345.     if (!m_pAdminSocket->Connect(m_pOptions->GetOption(IOPTION_LASTSERVERADDRESS), (UINT)m_pOptions->GetOptionVal(IOPTION_LASTSERVERPORT)) && WSAGetLastError() != WSAEWOULDBLOCK)
  1346.     {
  1347.         ShowStatus(_T("Error, could not connect to server"), 1);
  1348.         CloseAdminSocket();
  1349.     }
  1350. }